<?php
class Plus_Model_User_User
{
	protected $_id;
	protected $_login;
	protected $_email;
	protected $_password;
	protected $_idCountry;
	protected $_badLoginCounter;
	protected $_lastLoginTime;
	protected $_idUserPrivilage;
	protected $_dateCreated;
	
	protected $_mapper;
	
	public function __construct(array $options = null)
	{
		if (is_array($options)) {
			$this->setOptions($options);
		}
	}
	
	public function __set($name, $value)
	{
		$method = 'set' . $name;
		if (('mapper' == $name) || !method_exists($this, $method)) {
			throw new Exception('Invalid user property');
		}
		$this->$method($value);
	}
	
	public function __get($name)
	{
		$method = 'get' . $name;
		if (('mapper' == $name) || !method_exists($this, $method)) {
			throw new Exception('Invalid user property');
		}
		return $this->$method();
	}
	
	public function setOptions(array $options)
	{
		$methods = get_class_methods($this);
		foreach ($options as $key => $value) {
			$method = 'set' . ucfirst($key);
			if (in_array($method, $methods)) {
				$this->$method($value);
			}
		}
		return $this;
	}
	
	public function setMapper($mapper)
	{
		$this->_mapper = $mapper;
		return $this;
	}
	
	public function getMapper()
	{
		if (null === $this->getMapper()) {
			$this->setMapper(new Plus_Model_User_UserMapper());
		}
		return $this->_mapper;
	}
	
	public function save()
	{
		$this->getMapper()->save($this);
	}
	
	public function find($id)
	{
		$this->getMapper()->find($id, $this);
		return $this;
	}
	
	public function fetchAll()
	{
		return $this->getMapper()->fetchAll();
	}
	
	// TODO : add validation when setting data
	
	public function setId($id)
	{
		$this->_id = (int) $id;
		return $this;
	}
	
	public function getId()
	{
		return $this->_id;
	}
	
	public function setLogin($login)
	{
		$this->_login = (string) $login;
		return $this;
	}
	
	public function getLogin()
	{
		return $this->_login;
	}
	
	public function setEmail($email)
	{
		$this->_email = (string) $email;
		return $this;
	}
	
	public function getEmail()
	{
		return $this->_email;
	}
	
	public function setPassword($password)
	{
		$this->_password = (string) $password;
		return $this;
	}
	
	public function getPassword()
	{
		return $this->_password;
	}
	
	public function setIdCountry($idCountry)
	{
		$this->_idCountry = (int) $idCountry;
		return $this;
	}
	
	public function getIdCountry()
	{
		return $this->_idCountry;
	}
	
	public function setBadLoginCounter($badLoginCounter)
	{
		$this->_badLoginCounter = (int) $badLoginCounter;
		return $this;
	}
	
	public function getBadLoginCounter()
	{
		return $this->_badLoginCounter;
	}
	
	public function setLastLoginTime($lastLoginTime)
	{
		$this->_lastLoginTime = (int) $lastLoginTime;
		return $this;
	}
	
	public function getLastLoginTime()
	{
		return $this->_lastLoginTime;
	}
	
	public function setIdUserPrivilage($idUserPrivilage)
	{
		$this->_idUserPrivilage = (int) $idUserPrivilage;
		return $this;
	}
	
	public function getIdUserPrivilage()
	{
		return $this->_idUserPrivilage;
	}
	
	public function setDateCreated($dateCreated)
	{
		$this->_dateCreated = $dateCreated;
		return $this;
	}
	
	public function getDateCreated()
	{
		return $this->_dateCreated;
	}
}